package chat.model;

import com.orm.SugarRecord;

import java.util.ArrayList;
import java.util.List;

import model.MessageNotify;
import utils.TimeUtil;

/**
 * 未读消息,初始化是按时间倒序20条
 * Created by Administrator on 2016/3/30.
 */
public class NotifyMessageModel {
    // 最后一条未读消息的时间
    String downTime;

    List<MessageNotify> messageNotifies = new ArrayList<MessageNotify>();

    public void setMessageNotifies(List<MessageNotify> messageNotifies) {
        this.messageNotifies = messageNotifies;
    }

    // 刷新最新消息
    public void init()
    {
        List<MessageNotify> messages = SugarRecord.findWithQuery(MessageNotify.class, "Select * from MessageNotify  order by time desc limit 0,20");
        if (!messages.isEmpty())
        {
            MessageNotify messageNotify = messages.get(messages.size()-1);
            downTime = messageNotify.getTime();
            messageNotifies.clear();
            messageNotifies.addAll(messages);
        }
        else
        {
            downTime = TimeUtil.currentTime();
        }
    }

    // 获取更多的未读消息
    public void getMore()
    {
        List<MessageNotify> messages = SugarRecord.findWithQuery(MessageNotify.class, "Select * from MessageNotify where time < ?  order by time desc limit 0,15", downTime);
        if (!messages.isEmpty())
        {
            MessageNotify messageNotify = messages.get(messages.size()-1);
            downTime = messageNotify.getTime();
            messageNotifies.addAll(messages);
        }
    }

}
